package com.carepad.action.input;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.carepad.entity.Records;
import com.carepad.entity.Vital;
import com.carepad.service.RecordAnswerService;
import com.carepad.service.VitalRecordService;

public class RecordItemsInputSaveAction extends AbstractRecordItemsInputAction {

	private static final long serialVersionUID = 1L;

	private String ansRad;
	private String ansRadHidden;
	private String ansChk;
	private String ansChkHidden;
	private String ansTxtInt[];
	private String ansTxtIntHidden[];
	private String ansTxtDouble[];
	private String ansTxtDoubleHidden[];
	private String ansTxtSText[];
	private String ansTxtSTextHidden[];
	private String ansTxtLText[];
	private String ansTxtLTextHidden[];
	private String executionDateHidden;
	private String inputGroup;
	private String plannedTime;

	
	private List<Records> recordList = new ArrayList<Records>();
	private Vital vitalRecord = new Vital();
	
	@Qualifier("recordAnswerService")
	@Autowired
	protected RecordAnswerService recordAnswerService;

	@Qualifier("vitalRecordService")
	@Autowired
	protected VitalRecordService vitalRecordService;

	private InputStream inputStream;
	private Date execDate;

	public String execute() {

		if (!executionDateHidden.equals(getSessionImplementationDate()
				.getJaDate())) {
			setSessionExecutionDate(executionDateHidden);
		}

		execDate = getSessionImplementationDate().getEnDate();

		if (ansRad != null && ansRadHidden != null && ansRadHidden != "") {
			addRecordData(ansRadHidden, ansRad);
		}
		if (ansChk != null && ansChkHidden != null && ansChkHidden != "") {
			addRecordData(ansChkHidden, ansChk);
		}
		if (ansTxtInt != null && ansTxtIntHidden != null) {
			for (int i = 0; i < ansTxtInt.length; i++) {
				addRecordData(ansTxtIntHidden[i], ansTxtInt[i]);
			}
		}
		if (ansTxtDouble != null && ansTxtDoubleHidden != null) {
			for (int i = 0; i < ansTxtDouble.length; i++) {
				addRecordData(ansTxtDoubleHidden[i], ansTxtDouble[i]);
			}
		}
		if (ansTxtSText != null && ansTxtSTextHidden != null) {
			for (int i = 0; i < ansTxtSText.length; i++) {
				addRecordData(ansTxtSTextHidden[i], ansTxtSText[i]);
			}
		}
		if (ansTxtLText != null && ansTxtLTextHidden != null) {
			for (int i = 0; i < ansTxtLText.length; i++) {
				addRecordData(ansTxtLTextHidden[i], ansTxtLText[i]);
			}
		}

		try {
			if (inputGroup.equals(VITAL_INPUT)) {
				
				saveVitalData();
				
			} else {
				recordAnswerService.saveMultiple(recordList,
						getSessionUserVariable().getUsername());

			}
			inputStream = new ByteArrayInputStream("success".getBytes());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			inputStream = new ByteArrayInputStream("error".getBytes());
			return SUCCESS;
		}
	}

	private void addRecordData(String actionItem, String actionAnswer) {
		Records record = new Records();
		record.setActionItem(actionItem);
		record.setAnswer(actionAnswer);
		record.setExecutionDate(execDate);
		record.setActionTimeFrom(getActionTime(getPlannedTime()));
		record.setHcsId(getSessionPatient().getHcsID());
		record.setSessionId(getSessionId());
		recordList.add(record);
	}

	private void addVitalData(String item[], String answer[]) {
		for (int i = 0; i < item.length; i++) {
			if (item[i].equals(BLOOD_PRESSURE_HIGH)) {
				vitalRecord.setSbp(answer[i]);
			} else if (item[i].equals(BLOOD_PRESSURE_LOW)) {
				vitalRecord.setDbp(answer[i]);
			} else if (item[i].equals(BODY_TEMPERATURE)) {
				vitalRecord.setBt(answer[i]);
			} else if (item[i].equals(PULSE_RATE)) {
				vitalRecord.setPr(answer[i]);
			}
		}
		vitalRecord.setExecutionDate(execDate);
		vitalRecord.setHcsId(getSessionPatient().getHcsID());
		vitalRecord.setActionTimeFrom(getActionTime(getPlannedTime()));
	}
	
	public String saveVitalData() {
		
		execDate = getSessionImplementationDate().getEnDate();
		
		addVitalData(ansTxtIntHidden, ansTxtInt);
		addVitalData(ansTxtDoubleHidden, ansTxtDouble);
		vitalRecordService.save(vitalRecord, getSessionUserVariable().getUsername());
		
		return SUCCESS;
	}
	
	@SuppressWarnings("deprecation")
	private Date getActionTime(String plannedTime){
		try{
		String[] splitedTime = plannedTime.split(":");
		
		Date actionTimeFrom = getExecDate();
		
		actionTimeFrom.setHours(Integer.parseInt(splitedTime[0]));
		actionTimeFrom.setMinutes(Integer.parseInt(splitedTime[1]));
		actionTimeFrom.setSeconds(0);
		
		return actionTimeFrom;
		}
		catch (Exception e){
			return null;
		}
	}

	public String getAnsRad() {
		return ansRad;
	}

	public void setAnsRad(String ansRad) {
		this.ansRad = ansRad;
	}

	public String getAnsRadHidden() {
		return ansRadHidden;
	}

	public void setAnsRadHidden(String ansRadHidden) {
		this.ansRadHidden = ansRadHidden;
	}

	public String getAnsChk() {
		return ansChk;
	}

	public void setAnsChk(String ansChk) {
		this.ansChk = ansChk;
	}

	public String getAnsChkHidden() {
		return ansChkHidden;
	}

	public void setAnsChkHidden(String ansChkHidden) {
		this.ansChkHidden = ansChkHidden;
	}

	public String[] getAnsTxtInt() {
		return ansTxtInt;
	}

	public void setAnsTxtInt(String[] ansTxtInt) {
		this.ansTxtInt = ansTxtInt;
	}

	public String[] getAnsTxtIntHidden() {
		return ansTxtIntHidden;
	}

	public void setAnsTxtIntHidden(String[] ansTxtIntHidden) {
		this.ansTxtIntHidden = ansTxtIntHidden;
	}

	public String[] getAnsTxtDouble() {
		return ansTxtDouble;
	}

	public void setAnsTxtDouble(String[] ansTxtDouble) {
		this.ansTxtDouble = ansTxtDouble;
	}

	public String[] getAnsTxtDoubleHidden() {
		return ansTxtDoubleHidden;
	}

	public void setAnsTxtDoubleHidden(String[] ansTxtDoubleHidden) {
		this.ansTxtDoubleHidden = ansTxtDoubleHidden;
	}

	public String[] getAnsTxtSText() {
		return ansTxtSText;
	}

	public void setAnsTxtSText(String[] ansTxtSText) {
		this.ansTxtSText = ansTxtSText;
	}

	public String[] getAnsTxtSTextHidden() {
		return ansTxtSTextHidden;
	}

	public void setAnsTxtSTextHidden(String[] ansTxtSTextHidden) {
		this.ansTxtSTextHidden = ansTxtSTextHidden;
	}

	public String[] getAnsTxtLText() {
		return ansTxtLText;
	}

	public void setAnsTxtLText(String[] ansTxtLText) {
		this.ansTxtLText = ansTxtLText;
	}

	public String[] getAnsTxtLTextHidden() {
		return ansTxtLTextHidden;
	}

	public void setAnsTxtLTextHidden(String[] ansTxtLTextHidden) {
		this.ansTxtLTextHidden = ansTxtLTextHidden;
	}

	public List<Records> getRecordList() {
		return recordList;
	}

	public void setRecordList(List<Records> recordList) {
		this.recordList = recordList;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getExecutionDateHidden() {
		return executionDateHidden;
	}

	public void setExecutionDateHidden(String executionDateHidden) {
		this.executionDateHidden = executionDateHidden;
	}

	public Date getExecDate() {
		return execDate;
	}

	public void setExecDate(Date execDate) {
		this.execDate = execDate;
	}

	public String getInputGroup() {
		return inputGroup;
	}

	public void setInputGroup(String inputGroup) {
		this.inputGroup = inputGroup;
	}

	public Vital getVitalRecord() {
		return vitalRecord;
	}

	public void setVitalRecord(Vital vitalRecord) {
		this.vitalRecord = vitalRecord;
	}

	public String getPlannedTime() {
		return plannedTime;
	}

	public void setPlannedTime(String plannedTime) {
		this.plannedTime = plannedTime;
	}
	
}
